/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasymatchFacade;
import beans.ejb.FantasyrosterperweekFacade;
import beans.ejb.NflplayergameFacade;
import beans.ejb.WeekFacade;
import customEneities.Pair1;
import entities.Fantasyleague;
import entities.Fantasymatch;
import entities.Fantasyrosterperweek;
import entities.Fantasyteam;
import entities.Nflplayergame;
import entities.Week;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author stumbledog
 */
@Named(value = "gameCenterBean")
@SessionScoped
public class GameCenterBean implements Serializable {

    @EJB
    private WeekFacade weekFacade;
    @EJB
    private FantasymatchFacade fantasymatchFacade;
    @EJB
    private FantasyrosterperweekFacade fantasyrosterperweekFacade;
    @EJB
    private NflplayergameFacade nflplayergameFacade;
    
    private int weekNumber;
    private Week currentWeek;
    private Week selectedWeek;
    private Fantasyleague currentLeague;
    private List<Week> weekList;
    private List<List<Fantasymatch>> matchList;
    private Fantasymatch selectedMatch;

    public Fantasymatch getSelectedMatch() {
        return selectedMatch;
    }

    public void setSelectedMatch(Fantasymatch selectedMatch) {
        this.selectedMatch = selectedMatch;
    }
    private Fantasyteam homeTeam;
    private Fantasyteam awayTeam;
    private HtmlDataTable table1;
    private HtmlDataTable table2;
    private HtmlDataTable table3;
    private HtmlDataTable table4;
    private HtmlDataTable table5;
    private HtmlDataTable table6;
    private HtmlDataTable table7;
    private HtmlDataTable table8;
    private HtmlDataTable table9;
    private HtmlDataTable table10;
    private HtmlDataTable table11;
    private HtmlDataTable table12;
    private HtmlDataTable table13;
    private HtmlDataTable table14;
    
    private float homestaterpoints=0;
    private float homebenchpoints=0;
    private float awaystaterpoints=0;
    private float awaybenchpoints=0;
    
    private List<Pair1> homePlayers = new ArrayList<Pair1>();
    private List<Pair1> awayPlayers = new ArrayList<Pair1>();
    private List<Pair1> homeBNPlayers = new ArrayList<Pair1>();
    private List<Pair1> awayBNPlayers = new ArrayList<Pair1>();
    private List<HtmlDataTable> tableList = new ArrayList<HtmlDataTable>();
    private List<Fantasyrosterperweek> roster;
    
    private String homePositionPoint;
    private String awayPositionPoint;

    public String getHomePositionPoint() {
        Iterator<Pair1> it = homePlayers.iterator();
        int qb;
        int rb;
        
        while(it.hasNext()){
            Pair1 tmp = it.next();
            if(tmp.getGame()!=null){
                homestaterpoints+=tmp.getGame().getFantasyPoints().floatValue();
            }
        }
        return homePositionPoint;
    }

    public void setHomePositionPoint(String homePositionPoint) {
        this.homePositionPoint = homePositionPoint;
    }

    public String getAwayPositionPoint() {
        return awayPositionPoint;
    }

    public void setAwayPositionPoint(String awayPositionPoint) {
        this.awayPositionPoint = awayPositionPoint;
    }
    
    public List<Pair1> getHomeBNPlayers() {
        homeBNPlayers.clear();
        roster = fantasyrosterperweekFacade.findByTeamByWeek(selectedMatch.getHomeTeamId(), currentWeek);
        Iterator<Fantasyrosterperweek> itr = roster.iterator();
        
        while(itr.hasNext()){
            Fantasyrosterperweek tmp = itr.next();
            if("BN".equals(tmp.getFantasyPositionInRoster())){
                Nflplayergame game = nflplayergameFacade.findByPlayerByWeek(tmp.getPlayerId(), currentWeek);
                homeBNPlayers.add(new Pair1(tmp,game));
            }
        }
        return homeBNPlayers;
    }

    public float getHomestaterpoints() {
        homestaterpoints=0;
        Iterator<Pair1> it = homePlayers.iterator();
        
        while(it.hasNext()){
            Pair1 tmp = it.next();
            if(tmp.getGame()!=null){
                homestaterpoints+=tmp.getGame().getFantasyPoints().floatValue();
            }
        }
        return homestaterpoints;
    }

    public void setHomestaterpoints(float homestaterpoints) {
        this.homestaterpoints = homestaterpoints;
    }

    public float getHomebenchpoints() {
        homebenchpoints=0;
        Iterator<Pair1> it = homeBNPlayers.iterator();
        
        while(it.hasNext()){
            Pair1 tmp = it.next();
            if(tmp.getGame()!=null){
                homebenchpoints+=tmp.getGame().getFantasyPoints().floatValue();
            }
        }
        return homebenchpoints;
    }

    public void setHomebenchpoints(float homebenchpoints) {
        this.homebenchpoints = homebenchpoints;
    }

    public float getAwaystaterpoints() {
        awaystaterpoints=0;
        Iterator<Pair1> it = awayPlayers.iterator();
        
        while(it.hasNext()){
            Pair1 tmp = it.next();
            if(tmp.getGame()!=null){
                awaystaterpoints+=tmp.getGame().getFantasyPoints().floatValue();
            }
        }
        return awaystaterpoints;
    }

    public void setAwaystaterpoints(float awaystaterpoints) {
        this.awaystaterpoints = awaystaterpoints;
    }

    public float getAwaybenchpoints() {
        awaybenchpoints=0;
        Iterator<Pair1> it = awayBNPlayers.iterator();
        
        while(it.hasNext()){
            Pair1 tmp = it.next();
            if(tmp.getGame()!=null){
                awaybenchpoints+=tmp.getGame().getFantasyPoints().floatValue();
            }
        }
        return awaybenchpoints;
    }

    public void setAwaybenchpoints(float awaybenchpoints) {
        this.awaybenchpoints = awaybenchpoints;
    }
    
    public void setHomeBNPlayers(List<Pair1> homeBNPlayers) {
        this.homeBNPlayers = homeBNPlayers;
    }

    public List<Pair1> getAwayBNPlayers() {
        awayBNPlayers.clear();
        roster = fantasyrosterperweekFacade.findByTeamByWeek(selectedMatch.getAwayTeamId(), currentWeek);
        Iterator<Fantasyrosterperweek> itr = roster.iterator();
        
        while(itr.hasNext()){
            Fantasyrosterperweek tmp = itr.next();
            if("BN".equals(tmp.getFantasyPositionInRoster())){
                Nflplayergame game = nflplayergameFacade.findByPlayerByWeek(tmp.getPlayerId(), currentWeek);
                awayBNPlayers.add(new Pair1(tmp,game));
            }
        }
        return awayBNPlayers;
    }

    public void setAwayBNPlayers(List<Pair1> awayBNPlayers) {
        this.awayBNPlayers = awayBNPlayers;
    }
    
    public List<Pair1> getHomePlayers() {
        homePlayers.clear();
        roster = fantasyrosterperweekFacade.findByTeamByWeek(selectedMatch.getHomeTeamId(), currentWeek);
        Iterator<Fantasyrosterperweek> itr = roster.iterator();
        
        while(itr.hasNext()){
            Fantasyrosterperweek tmp = itr.next();
            if(!"BN".equals(tmp.getFantasyPositionInRoster())){
                Nflplayergame game = nflplayergameFacade.findByPlayerByWeek(tmp.getPlayerId(), currentWeek);
                homePlayers.add(new Pair1(tmp,game));
            }
        }
        return homePlayers;
    }

    public List<Pair1> getAwayPlayers() {
        awayPlayers.clear();
        roster = fantasyrosterperweekFacade.findByTeamByWeek(selectedMatch.getAwayTeamId(), currentWeek);
        Iterator<Fantasyrosterperweek> itr = roster.iterator();
        
        while(itr.hasNext()){
            Fantasyrosterperweek tmp = itr.next();
            if(!"BN".equals(tmp.getFantasyPositionInRoster())){
                Nflplayergame game = nflplayergameFacade.findByPlayerByWeek(tmp.getPlayerId(), currentWeek);
                awayPlayers.add(new Pair1(tmp,game));
            }
        }
        return awayPlayers;
    }
    public void setHomePlayers(List<Pair1> homePlayers) {
        this.homePlayers = homePlayers;
    }

    

    public void setAwayPlayers(List<Pair1> awayPlayers) {
        this.awayPlayers = awayPlayers;
    }
    
    public HtmlDataTable getTable2() {
        return table2;
    }

    public void setTable2(HtmlDataTable table2) {
        this.table2 = table2;
    }

    public HtmlDataTable getTable3() {
        return table3;
    }

    public void setTable3(HtmlDataTable table3) {
        this.table3 = table3;
    }

    public HtmlDataTable getTable4() {
        return table4;
    }

    public void setTable4(HtmlDataTable table4) {
        this.table4 = table4;
    }

    public HtmlDataTable getTable5() {
        return table5;
    }

    public void setTable5(HtmlDataTable table5) {
        this.table5 = table5;
    }

    public HtmlDataTable getTable6() {
        return table6;
    }

    public void setTable6(HtmlDataTable table6) {
        this.table6 = table6;
    }

    public HtmlDataTable getTable7() {
        return table7;
    }

    public void setTable7(HtmlDataTable table7) {
        this.table7 = table7;
    }

    public HtmlDataTable getTable8() {
        return table8;
    }

    public void setTable8(HtmlDataTable table8) {
        this.table8 = table8;
    }

    public HtmlDataTable getTable9() {
        return table9;
    }

    public void setTable9(HtmlDataTable table9) {
        this.table9 = table9;
    }

    public HtmlDataTable getTable10() {
        return table10;
    }

    public void setTable10(HtmlDataTable table10) {
        this.table10 = table10;
    }

    public HtmlDataTable getTable11() {
        return table11;
    }

    public void setTable11(HtmlDataTable table11) {
        this.table11 = table11;
    }

    public HtmlDataTable getTable12() {
        return table12;
    }

    public void setTable12(HtmlDataTable table12) {
        this.table12 = table12;
    }

    public HtmlDataTable getTable13() {
        return table13;
    }

    public void setTable13(HtmlDataTable table13) {
        this.table13 = table13;
    }

    public HtmlDataTable getTable14() {
        return table14;
    }

    public void setTable14(HtmlDataTable table14) {
        this.table14 = table14;
    }
    
    public HtmlDataTable getTable1() {
        return table1;
    }

    public void setTable1(HtmlDataTable table1) {
        this.table1 = table1;
    }

    public void selectMatch1() {
        selectedMatch = (Fantasymatch) table1.getRowData();
    }
    public void selectMatch2() {
        selectedMatch = (Fantasymatch) table2.getRowData();
    }
    public void selectMatch3() {
        selectedMatch = (Fantasymatch) table3.getRowData();
    }
    public void selectMatch4() {
        selectedMatch = (Fantasymatch) table4.getRowData();
    }
    public void selectMatch5() {
        selectedMatch = (Fantasymatch) table5.getRowData();
    }
    public void selectMatch6() {
        selectedMatch = (Fantasymatch) table6.getRowData();
    }
    public void selectMatch7() {
        selectedMatch = (Fantasymatch) table7.getRowData();
    }
    public void selectMatch8() {
        selectedMatch = (Fantasymatch) table8.getRowData();
    }
    public void selectMatch9() {
        selectedMatch = (Fantasymatch) table9.getRowData();
    }
    public void selectMatch10() {
        selectedMatch = (Fantasymatch) table10.getRowData();
    }
    public void selectMatch11() {
        selectedMatch = (Fantasymatch) table11.getRowData();
    }
    public void selectMatch12() {
        selectedMatch = (Fantasymatch) table12.getRowData();
    }
    public void selectMatch13() {
        selectedMatch = (Fantasymatch) table13.getRowData();
    }
    public void selectMatch14() {
        selectedMatch = (Fantasymatch) table14.getRowData();
    }

    public HtmlDataTable getTableList(int i) {
        return tableList.get(i);
    }

    public void setTableList(List<HtmlDataTable> tableList) {
        this.tableList = tableList;
    }

    public void selectMatch(int i) {
        selectedMatch = (Fantasymatch) tableList.get(i).getRowData();
    }
    
    public List<List<Fantasymatch>> getMatchList() {
        weekNumber = 0;
        tableList = new ArrayList<HtmlDataTable>();
        matchList = new ArrayList<List<Fantasymatch>>();
        getWeekList();
        Iterator<Week> weekIT = weekList.iterator();
        while (weekIT.hasNext()) {
            Week tmpweek = weekIT.next();
            List<Fantasymatch> tmpmatchList = fantasymatchFacade.findMatchResultByLeagueIdWeekId(currentLeague, tmpweek);
            matchList.add(tmpmatchList);
            if(selectedMatch==null){
                selectedMatch=tmpmatchList.get(0);
            }
        }
        return matchList;
    }

    public int getWeekNumber() {
        weekNumber++;
        weekNumber = (weekNumber - 1) % 14 + 1;
        return weekNumber;
    }

    public void setMatchList(List<List<Fantasymatch>> matchList) {
        this.matchList = matchList;
    }

    public List<Week> getWeekList() {
        weekList = new ArrayList<Week>();
        for (int i = 1; i <= 14; i++) {
            if (i < 10) {
                weekList.add(weekFacade.getWeek("2011", "0" + String.valueOf(i)));
            } else {
                weekList.add(weekFacade.getWeek("2011", String.valueOf(i)));
            }
        }
        return weekList;
    }

    public void setWeekList(List<Week> weekList) {
        this.weekList = weekList;
    }

    /**
     * Creates a new instance of GameCenterBean
     */
    public GameCenterBean() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean) request.getSession().getAttribute("accountBean");
        currentWeek = b.getCurrentWeek();
        currentLeague = b.getCurrentLeague();
        System.out.println("GameCenter constructed");
        
    }
}
